// eee.Sheffield.PZ.Imaging
//
// Copyright ?Ping Zou, 2007
// sg71.cherub@gmail.com

using System;
using System.Collections.Generic;
using System.Text;
using eee.Sheffield.PZ.Math;
using System.Drawing;
using System.Drawing.Imaging;

namespace eee.Sheffield.PZ.Imaging.ImageFilter
{
    public class GVFThinning
    {
        #region Flag
        private string flag = "[Image -> Morphology -> GVFThinning]";
        public string Flag() { return flag; }
        #endregion

        #region Fields
        // RJMCMC exampler
        private GVFThinningSampler _sampler;

        // data term
        private Bitmap _srcImage;
        private PZMath_matrix _gvfU;
        private PZMath_matrix _gvfV;
        private PZMath_matrix _gvfMagnitude;

        // line segment pool
        private LineSegmentConfiguration _pool;
        #endregion

        #region Constructor
        public GVFThinning(Bitmap image, PZMath_matrix u, PZMath_matrix v, PZMath_matrix magnitude, LineSegmentConfiguration all)
        {
            _srcImage = image;
            _gvfU = u;
            _gvfV = v;
            _gvfMagnitude = magnitude;
            _pool = all;
        }
        #endregion

        #region initialize method
        public void Init(int N, int n0, double lamda, int width, int height)
        {
            _sampler = new GVFThinningSampler(N, n0, lamda, width, height);
        }
        #endregion

        #region GVF thinning
        public Bitmap Apply()
        {            
            // prepare target distribution
            TargetDistribution targetDistribution = new TargetDistribution();
            targetDistribution.TargetDensityFunction = new targetDensityFunction(GVFThinningSampler.ExampleDensityFunction);

            // assign target distribution and instrumental distribution to the work space
            _sampler.GMTargetDistribution = targetDistribution;

            // M-H sample
            _sampler.Init(_pool);
            List<LineSegmentConfiguration> samples = _sampler.Sample();
            int returnIndex = _sampler.IndexMax;            
            return samples[returnIndex].DrawLineSegments(_srcImage);                
        } // Apply()
        #endregion


    }
}
